package com.jkl.controller.button;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jkl.entity.BaseResponseDTO;
import com.jkl.entity.bsWorkflowLog.BsWorkflowLog;
import com.jkl.entity.button.*;
import com.jkl.entity.menu.BsMenu;
import com.jkl.service.button.inter.ButtonServiceInter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuzhennan
 * @create 2021-03-22 16:15
 */
@RestController
@RequestMapping("/jky/button")
@Slf4j
public class ButtonController {

    @Autowired
    private ButtonServiceInter buttonServiceInter;

    @PostMapping("/addButton")
    public int addButton(@RequestBody Button button){
        if (button.getMenuId() == null){
            return 2;
        }
        Button button1 = buttonServiceInter.selectButtonByMenuIdAndButtonCode(button);
        if (button1 == null){
            return buttonServiceInter.addButton(button);
        }
        return 0;
    }

    @PostMapping("/selectButtonListByMenuId")
    public List<Button> selectButtonListByMenuId(@RequestBody Integer menuId){
        List<Button> buttons = buttonServiceInter.selectButtonListByMenuId(menuId);
        return buttons;
    }

//    @PostMapping("/addButtonAndRole")
//    public int addButtonAndRole(@RequestBody ButtonVo buttonVo){
//        BsRoleButton bsRoleButton = buttonServiceInter.selectButtonAndRole(buttonVo);
//        if (null == bsRoleButton || bsRoleButton.getStatus() == 0){
//            //增加
//            buttonServiceInter.addButtonAndRole(buttonVo);
//            return 1;
//        }else{
//            //删除
//            buttonServiceInter.deleteButtonAndRole(buttonVo);
//            return 2;
//        }
//    }

    @PostMapping("/selectAllButtonWithTree")
    public BaseResponseDTO selectAllButtonWithTree(){
        BaseResponseDTO returnData = new BaseResponseDTO();
        List<ButtonVo> bsMenuList = buttonServiceInter.selectAllButtonWithTree();
        JSONObject returnJson = new JSONObject();
        JSONArray deptArray = (JSONArray) JSONArray.toJSON(bsMenuList);
        returnJson.put("bsMenuList", deptArray);
        returnData.setReturnData(returnJson);
        return returnData;
    }

    @PostMapping("addButtonAndRole")
    public void addButtonAndRole(@RequestBody List<ButtonPo> buttonPo){
        //先清空role_id的bs_role_button
        Integer roleId = buttonPo.get(0).getRoleId();
        buttonServiceInter.deleteAllButtonByRole(roleId);
        for (ButtonPo po : buttonPo) {
            buttonServiceInter.addButtonAndRole(po);
        }

    }
    @GetMapping("/getButtonByRoleId")
    public BaseResponseDTO getButtonByRoleId(@RequestParam int roleId){
        BaseResponseDTO returnData = new BaseResponseDTO();
        List<Integer> integers = buttonServiceInter.getButtonByRoleId(roleId);
        JSONObject returnJson = new JSONObject();
        JSONArray deptArray = (JSONArray) JSONArray.toJSON(integers);
        returnJson.put("bsMenuList", deptArray);
        returnData.setReturnData(returnJson);
        return returnData;
    }
}
